package com.example.blog.service;

import com.example.blog.mapper.BlogInfoMapper;
import com.example.blog.mapper.UserInfoMapper;
import com.example.blog.model.BlogInfo;
import com.example.blog.model.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Home-pc
 * Date: 2024-02-27
 * Time: 14:08
 */
@Service
public class UserService {
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private BlogInfoMapper blogInfoMapper;

    public UserInfo queryByName(String userName){
        return userInfoMapper.queryByName(userName);
    }

    public UserInfo queryById(Integer userId) {
        return userInfoMapper.queryById(userId);
    }

    public UserInfo getAuthorInfo(Integer blogId) {
        //如何通过博客id获取作者的信息：先通过博客id获取博客信息，通过博客信息，拿到博客作者的id
        //，再通过作者的id，查询作者的信息
        BlogInfo blogInfo = blogInfoMapper.queryById(blogId);
        if(blogInfo == null || blogInfo.getUserId()<0){
            return null;
        }
        return  userInfoMapper.queryById(blogInfo.getUserId());
    }
}
